﻿package com.ufenlei.LTS{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.display.Sprite;

	public class Scroll extends MovieClip{
		private var _sh:MovieClip;
		private var _sb:MovieClip;
		private var _su:MovieClip;
		private var _sd:MovieClip;
		private var _barHeight:uint;
		private var _arrowHeight:uint;
		private var _handleHeight:uint;
		public static const UPDATE:String = "ScrollUpdate";
		
		public function Scroll(h:uint){
			_barHeight = h;
			_handleHeight = 30;
			_arrowHeight = 20;
			initScroll(scrollHandle,scrollBar,scrollUpArrow, scrollDownArrow);
			initEvent();
		}
		public function moveBy(bh:int = 10):void{
			setY(_sh.y + bh);
		}
		public function set barHeight(bh:uint):void{
			_barHeight = bh;
			initScroll();
		}
		//设置滚动高度，自适应
		public function set scrollHeight(sh:int):void{
			//trace("scrollHeight",sh);
			handleHeight = _barHeight - _arrowHeight*2 - sh;
		}
		public function set handleHeight(hh:int):void{
			_handleHeight = Math.max(10,Math.min( _barHeight - 2*_arrowHeight,hh));			
			initScroll();
			initEvent();
		}
		public function scrollTo(p:Number):void{
			p = Math.min(1.0 , Math.max(0,p));
			setY( _arrowHeight + p*(_barHeight - 2 * _arrowHeight - _handleHeight));
		}
		private function initScroll(sh:MovieClip = null, 
									sb:MovieClip = null, 
									su:MovieClip = null, 
									sd:MovieClip = null):void{
			if(sh !== null){
				_sh = sh;
				_sb = sb;
				_su = su;
				_sd = sd;
			}	
			_sb.height = _barHeight;
			_sh.height = _handleHeight;
			_sh.alpha = (_handleHeight == _barHeight-2*_arrowHeight)?0:1.0;
			_sd.y = _barHeight - _arrowHeight;
		}
		private function initEvent():void{
			this._sh.addEventListener(MouseEvent.MOUSE_DOWN,onMDHandle);
			this._sb.addEventListener(MouseEvent.MOUSE_DOWN,onMDBar);
			this._su.addEventListener(MouseEvent.CLICK,onCUp);
			this._sd.addEventListener(MouseEvent.CLICK,onCDown);
		}
		private function onCUp(evt:MouseEvent):void{
			setY(this._sh.y - 20);
		}
		private function onCDown(evt:MouseEvent):void{
			setY(this._sh.y + 20);
		}
		private function onMDBar(evt:MouseEvent):void{
			var g0:Point = _sb.localToGlobal(new Point(0,0)) ;
			setY(evt.stageY -g0.y - _handleHeight/2);
		}
		private function onMDHandle(evt:MouseEvent):void{
			//trace("onMDHandle");
			(root as MovieClip).addEventListener(MouseEvent.MOUSE_UP,onMUHandle);
			var h:int = _barHeight-_arrowHeight*2-_handleHeight;
			var xr:Rectangle = new Rectangle( 0, _arrowHeight, 0, h);
			_sh.startDrag(	false, xr );
			this.addEventListener(Event.ENTER_FRAME, onEE);
		}
		private function onMUHandle(evt:MouseEvent):void{
			//trace("onMUHandle",this._sh.y);
			(root as MovieClip).removeEventListener(MouseEvent.MOUSE_UP,onMUHandle);
			this.removeEventListener(Event.ENTER_FRAME, onEE);
			_sh.stopDrag();
		};
		private function setY(hy:int = 0):void{
			var m:int = _barHeight - _arrowHeight - _handleHeight;
			this._sh.y = Math.min(m, Math.max(_arrowHeight, hy));
			updateEvent();
		}
		private function onEE(evt:Event):void{
			updateEvent();
		}
		private function updateEvent():void{
			this.dispatchEvent(new ProgressEvent(UPDATE,
												 false,
												 false,
												 (_sh.y - _arrowHeight),
												 (_barHeight-2*_arrowHeight - _handleHeight)));
		}
	}
}